package com.gem.eolane.interceptor;

import com.gem.eolane.entity.Admin;
import com.gem.eolane.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//拦截器
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取访问资源路径

        String url = request.getRequestURI();
        /*if(url.indexOf("/assets")>=0 && url.indexOf("/css")>=0 && url.indexOf("/js")>=0 && url.indexOf("/font")>=0 && url.indexOf("/images")>=0){
            return true;
        }*/
        System.out.println("资源路径：：：：：：：："+url);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Admin admin = (Admin) session.getAttribute("admin");
        System.out.println("user++++:::::::::"+user);
        System.out.println("admin++++:::::::::"+admin);

        if(user != null && url.indexOf("/f/u")>=0){//放行用户
            return true;

        }
        if(user == null && url.indexOf("/f/u/")>=0){//未登录访问被拦截前台路径，不放行
          /* request.getRequestDispatcher("/f/t/login").forward(request,response);*/
          response.sendRedirect("/eolane/f/t/login");
        }
        return false;

    }

}
